package com.jie.controller.admin;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jie.pojo.Comment;
import com.jie.pojo.User;
import com.jie.service.BlogService;
import com.jie.service.CommentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 曾豪杰
 * @Date: 2022/09/16/12:44
 * @Description:
 */
@Controller
@Slf4j
public class CommentController {

    @Autowired
    private CommentService commentService;
    @Autowired
    private BlogService blogService;

    @Value("${picture}")
    private String picture;

    @GetMapping("/comments/{blogId}")
    public String comments(@PathVariable Long blogId, Model model){
        LambdaQueryWrapper<Comment> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Comment::getCreateTime);
        queryWrapper.eq(Comment::getBlogId,blogId);
        queryWrapper.isNull(Comment::getParentCommentId);
        List<Comment> commentList = commentService.list(queryWrapper);

        for (Comment comment : commentList) {
            LambdaQueryWrapper<Comment> queryWrapper1=new LambdaQueryWrapper<>();
            queryWrapper1.orderByAsc(Comment::getCreateTime);
            queryWrapper1.eq(Comment::getBlogId,blogId);
            queryWrapper1.eq(Comment::getParentCommentId,comment.getId());
            List<Comment> replyList = commentService.list(queryWrapper1);
            comment.setReplyComment(replyList);
        }

        model.addAttribute("comments",eachComment(commentList));
        return "blog :: commentList";
    }

    @Transactional
    @PostMapping("/comments")
    public String post(Comment comment, HttpSession session){
        Long id1 = comment.getBlog().getId();
        comment.setBlog(blogService.getById(id1));
        Long id = comment.getParentComment().getId();
        if(id!=-1){
            comment.setParentComment(commentService.getById(id));
            comment.setParentCommentId(commentService.getById(id).getId());
        }else {
            comment.setParentComment(null);
        }
        comment.setBlogId(id1);

        User user = (User) session.getAttribute("user");
        if(user!=null|| Objects.equals(comment.getNickName(), "admins")){
            comment.setPicture("/images/photo.jpg");
            comment.setNickName("杰");//管理员
            comment.setAdmin(1);
        }else {
            comment.setPicture(picture);//普通游客
        }
        commentService.save(comment);
        return "redirect:/comments/"+id1;
    }
    /**
    * @Description: 复制
    * @Param: [comments]
    * @return: java.util.List<com.jie.pojo.Comment>
    * @Author: 曾豪杰
    * @Date: 2022/9/17
    */
    public List<Comment> eachComment(List<Comment> comments){
        List<Comment> commentList=new ArrayList<>();
        for (Comment comment : comments) {
            Comment c=new Comment();
            BeanUtils.copyProperties(comment,c);
            commentList.add(c);
        }
        //合并各层子代到一个集合中
        combbineChildren(commentList);
        return commentList;
    }
    /**
    * @Description: 进行回复评论的迭代更新
    * @Param: [comments]
    * @return: void
    * @Author: 曾豪杰
    * @Date: 2022/9/17
    */
    public void combbineChildren(List<Comment> comments){
        for (Comment comment : comments) {
            List<Comment> replyComment = comment.getReplyComment();
            for (Comment comment1 : replyComment) {
                //循环迭代找出子代放在集合tempReplys中
                recursively(comment1);
            }
            //修改顶级的reply为迭代后的reply
            comment.setReplyComment(tempReplys);
            //清楚临时区
            tempReplys=new ArrayList<>();
        }
    }
    //存放迭代后的集合
    private List<Comment> tempReplys=new ArrayList<>();
    /**
    * @Description: 递归迭代 剥洋葱 所有子集到第二层
    * @Param: [comment]
    * @return: java.util.List<com.jie.pojo.Comment>
    * @Author: 曾豪杰
    * @Date: 2022/9/17
    */
    public void recursively(Comment comment){
        tempReplys.add(comment);//顶节点添加到临时存放区
        if (comment.getReplyComment()!=null&&comment.getReplyComment().size()>0){
            List<Comment> replyComment = comment.getReplyComment();
            for (Comment comment1 : replyComment) {
                tempReplys.add(comment1);
                if (comment.getReplyComment().size()>0){
                    recursively(comment1);
                }
            }
        }
    }

}
